<?php
namespace app\restaurant\controller;

use app\ApiRest;

use app\restaurant\model\ClassConnect;
use app\restaurant\model\ClassDate;
use app\restaurant\model\Config;
use app\restaurant\model\CouponRecord;
use app\restaurant\model\Evaluate;
use app\restaurant\model\OrderAddress;
use app\restaurant\model\OrderGoods;
use app\restaurant\model\Progress;
use app\restaurant\model\RefundOrder;
use app\restaurant\model\ResClass;
use app\restaurant\model\StoreStaff;
use app\restaurant\model\Table;
use app\restaurant\model\TransporterOrder;
use app\restaurant\server\Context;
use app\restaurant\server\DadaApi;
use think\App;

use app\restaurant\model\Car as model;

use app\restaurant\model\Printer;

use app\restaurant\model\Goods;

use app\restaurant\model\FeedingCar;

use app\restaurant\model\Order;

use app\restaurant\model\Store;


use think\facade\Db;


class IndexStaff extends ApiRest
{

    protected $model;

    protected $staff_model;

    protected $printer_model;

    protected $goods_model;

    protected $feeding_car_model;

    protected $store_info;

    protected $order_model;

    protected $store_model;

    protected $refund_model;


    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

        $this->staff_model = new StoreStaff();

        $this->printer_model = new Printer();

        $this->goods_model = new Goods();

        $this->order_model = new Order();

        $this->refund_model = new RefundOrder();

        $this->feeding_car_model = new FeedingCar();

        $this->store_model = new Store();

        $store = $this->staff_model->getStaffStore($this->getUserId(),3);

        if(empty($store)){

            $this->errorMsg('你不是门店员工');

        }
        $dis[] = ['id','=',$store['store_id']];

        $dis[] = ['status','>',-1];

        $store = $this->store_model->storeInfo($dis);

        if(empty($store)){

            $this->errorMsg('门店已经关闭');
        }

        $this->store_info = $store;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-16 11:24
     * @功能说明:打印机列表
     */
    public function printerList(){

        $dis[] = ['store_id','=',$this->store_info['id']];

        $dis[] = ['status','>',-1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        $data = $this->printer_model->dataList($dis,10);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-20 11:09
     * @功能说明:添加打印机
     */
    public function printerAdd(){

        $input = $this->_input;

        $input['store_id'] = $this->store_info['id'];

        $input['uniacid']  = $this->_uniacid;

        $res = $this->printer_model->dataAdd($input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-20 11:31
     * @功能说明:编辑打印机
     */
    public function printerUpdate(){

        $input = $this->_input;

        $res = $this->printer_model->dataUpdate(['id'=>$input['id']],$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-24 18:29
     * @功能说明:员工端门店详情
     */
    public function staffStoreInfo(){

        $goods_model = new Goods();

        $list = $this->order_model->storeOrderFrom($this->store_info['id'],'',$this->_uniacid);
        //订单实际金额
        $data['true_price']  = $list['true_price'];
        //订单总量
        $data['order_count'] = $list['order_count'];
        //商品总量
        $data['goods_num']   = $goods_model->where(['store_id'=>$this->store_info['id']])->where('status','>',-1)->count();
        //门店详情
        $data['store_info']  = $this->store_info;

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:29
     * @功能说明:编辑
     */
    public function goodsUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $data = $this->goods_model->goodsUpdate($dis,$input);

        return $this->success($data);

    }




    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:35
     * @功能说明:员工端订单列表
     */
    public function staffOrderList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.staff_id','=',$this->getUserId()];

        $dis[] = ['a.transaction_id','<>',0];

        $dis[] = ['a.pay_type','>',0];
        //状态
        if(!empty($input['pay_type'])){
            //商家待接单
            if($input['pay_type']==1){

                $dis[] = ['g.status','=',0];

            //骑手待接单
            }elseif ($input['pay_type']==2){

                $dis[] = ['g.status','=',1];

            //配送中
            } elseif($input['pay_type']==3){

                $dis[] = ['g.status','in',[2,3,100]];

            }else{
                //
                $dis[] = ['g.status','=',$input['pay_type']];
                //订单异常
                if($input['pay_type']==5){
                    //只有三方平台的问题才叫异常
                    $dis[] = ['g.cancel_from','in',[1,3,4]];

                }

            }

        }
        if(isset($input['order_type'])){

            $dis[] = ['a.order_type','=',$input['order_type']];

        }
        $where = [];
        //商品
        if(!empty($input['goods_name'])){

            $where[] = ['f.goods_name','like','%'.$input['goods_name'].'%'];

            $where[] = ['a.order_code','like','%'.$input['goods_name'].'%'];
        }

        $data = $this->order_model->staffOrderList($dis,$where);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-24 18:52
     * @功能说明:营业数据
     */
    public function listFrom(){

        $toDay = date('Y-m-d',time());

        $yesterday = date('Y-m-d',time()-86400);
        //昨日
        $data['yesterday'] = $this->order_model->storeOrderFrom($this->store_info['id'],$yesterday,$this->_uniacid);
        //今日
        $data['to_day']    = $this->order_model->storeOrderFrom($this->store_info['id'],$toDay,$this->_uniacid);
        //所有
        $data['all']       = $this->order_model->storeOrderFrom($this->store_info['id'],'',$this->_uniacid);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-24 18:58
     * @功能说明:商品排行
     */
    public function goodsTop(){

        $input = $this->_input;

        $start_time = !empty($input['start_time'])?$input['start_time']:0;

        $end_time   = !empty($input['end_time'])?$input['end_time']:0;

        $data  = $this->goods_model->goodsTop($this->store_info['id'],$input['sort'],$start_time,$end_time);

        return $this->success($data);

    }




    /**
     * @author chenniang
     * @DataTime: 2020-10-26 10:01
     * @功能说明:门店修改
     */
    public function storeUpdate(){

        $input  = $this->_input;

        $res = $this->store_model->where(['id'=>$this->store_info['id']])->update($input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-20 09:48
     * @功能说明:员工端商品列表
     */
    public function staffGoodsList(){

        $input = $this->_param;


        $dis[] = ['a.status','>',-1];

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['cate_id'])){

            $dis[] = ['a.cate_id','=',$input['cate_id']];
        }

        $dis[] = ['a.store_id','=',$this->store_info['id']];

        if(!empty($input['title'])){

            $dis[] = ['a.title','like','%'.$input['title'].'%'];

        }

        $data  = $this->goods_model->getStaffGoodsList($dis,$input['sort']);

        return $this->success($data);


    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-26 17:25
     * @功能说明:自提商品核销
     */
    public function selfOrderHx(){

        $input = $this->_input;

        $order = $this->order_model->orderInfo(['id'=>$input['id']]);

        if(empty($order)){

            $this->errorMsg('订单不存在');
        }

        if($order['store_id']!=$this->store_info['id']){

            $title = $this->store_model->where(['id'=>$order['store_id']])->value('title');

            $this->errorMsg('只有'.$title.'员工才能核销');

        }
        //0是外卖 1是堂食 2是自提
        if($order['order_type']!=2){

            $this->errorMsg('只有自提订单才能核销');

        }

        if($order['pay_type']!=1){

            $this->errorMsg('订单状态错误');

        }

        $config_model = new Config();

        $config = $config_model->configInfo(['uniacid'=>$order['uniacid']]);

        if($config['hx_pass']!=$input['hx_pass']){

            $this->errorMsg('密码错误');
        }

        $transporter_model = new TransporterOrder();

        $transporter_model->where(['order_id'=>$input['id']])->update(['status'=>4]);

        $progress_model = new Progress();
        //添加用户收货成功进度
        $progress_model->userGetGoods($order['uniacid'],$order['order_code']);
        //修改订单状态
        $this->order_model->endOrder($input['id'],$this->getUserId());

        return $this->success(true);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-03 09:27
     * @功能说明:获取物流类
     */
    public function getClass(){

        $calss  = new DadaApi();

        return $calss;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-30 10:28
     * @功能说明:商家接单(发单)
     */
    public function acceptOrder(){

        $input = $this->_input;

        $context = new Context($input['id']);

        Db::startTrans();

        $res = $context->addOrder($input['id']);

        if(!empty($res['code'])&&$res['code']==500){

            $this->errorMsg($res['msg']);
        }

        if(isset($res['code'])&&$res['code']!=0){

            $this->errorMsg($res['msg']);

        }

        Db::commit();

        return $this->success($res['code']);

    }




    /**
     * @author chenniang
     * @DataTime: 2020-11-03 09:26
     * @功能说明:商家重新发单
     */
    public function reAcceptOrder(){

        $input = $this->_input;

        $context = new Context($input['id']);

        Db::startTrans();

        $res = $context->reAddOrder($input['id']);

        if(!empty($res['code'])&&$res['code']==500){

            $this->errorMsg($res['msg']);
        }

        if(isset($res['code'])&&$res['code']!=0){

            $this->errorMsg($res['msg']);

        }

        Db::commit();

        return $this->success($res['code']);


    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-04 14:48
     * @功能说明:商家统一退款
     */
    public function agreeRefund(){

        $input = $this->_input;

        $refund_order = new RefundOrder();

        $res = $refund_order->agreeRefund($input['id'],$input['price'],$this->payConfig($this->_uniacid));

        return $this->success($res);


    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-04 14:48
     * @功能说明:商家统一退款
     */
    public function noAgreeRefund(){

        $input = $this->_input;

        $refund_order = new RefundOrder();

        $res = $refund_order->noAgreeRefund($input['id']);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-04 16:54
     * @功能说明:商家取消三方平台的订单
     */
    public function cancelSendOrder(){

        $input = $this->_input;

        $context = new Context($input['order_code']);

        $res = $context->orderCancel($input['order_code']);

        if(isset($res['code'])&&$res['code']!=0){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);


    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-05 09:29
     * @功能说明:商家端退款订单
     */
    public function refundOrderList(){
        //审核过期售后订单
        $this->refund_model->checkOrder($this->_uniacid,$this->payConfig($this->_uniacid));

        $input = $this->_input;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.user_cancel','=',0];

        $dis[] = ['a.store_id','=',$this->store_info['id']];

        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];
        }

        if(isset($input['order_type'])){

            $dis[] = ['b.order_type','=',$input['order_type']];
        }

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['c.goods_name','like','%'.$input['name'].'%'];

            $where[] = ['b.order_code','like','%'.$input['name'].'%'];

            $where[] = ['a.order_code','like','%'.$input['name'].'%'];
        }

        $data = $this->refund_model->refundOrderList($dis,$where);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-06 14:04
     * @功能说明:商家备货成功
     */
    public function StoreStockUp(){

        $input = $this->_input;

        $order = $this->order_model->orderInfo(['id'=>$input['id']]);

        $transporter_model = new TransporterOrder();

        $progress_model    = new Progress();

        $transporter_model->where(['order_id'=>$input['id']])->update(['status'=>2]);

        $res = $progress_model->storeStockUp($order['uniacid'],$order['order_code']);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-10 15:14
     * @功能说明:商家配送的时候 商家作为骑手接单
     */
    public function riderAcceptOrder(){

        $input = $this->_input;

        $order = $this->order_model->orderInfo(['id'=>$input['id']]);

        $transporter_model = new TransporterOrder();

        $progress_model    = new Progress();

        $transporter_model->where(['order_id'=>$input['id']])->update(['status'=>2,'transporter_name'=>$this->store_info['title'],'transporter_phone'=>$this->store_info['mobile']]);

        $res = $progress_model->riderAcceptOrder($order['uniacid'],$order['order_code']);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-10 15:14
     * @功能说明:商家配送的时候 商家作为骑手接货
     */
    public function riderGoodsUp(){

        $input = $this->_input;

        $order = $this->order_model->orderInfo(['id'=>$input['id']]);

        $transporter_model = new TransporterOrder();

        $progress_model    = new Progress();

        $transporter_model->where(['order_id'=>$input['id']])->update(['status'=>3,'transporter_name'=>$this->store_info['title'],'transporter_phone'=>$this->store_info['mobile']]);
        //骑手接单
        $res = $progress_model->riderAcceptOrder($order['uniacid'],$order['order_code']);
        //骑手取货
        $res = $progress_model->riderGoodsUp($order['uniacid'],$order['order_code']);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-10 15:14
     * @功能说明:商家配送的时候 商家作为骑手送货到达
     *
     */
    public function riderGoodsSuccess(){

        $input = $this->_input;

        $order = $this->order_model->orderInfo(['id'=>$input['id']]);

        $transporter_model = new TransporterOrder();

        $progress_model    = new Progress();

        $transporter_model->where(['order_id'=>$input['id']])->update(['status'=>4,'finish_time'=>time(),'transporter_name'=>$this->store_info['title'],'transporter_phone'=>$this->store_info['mobile']]);
        //商品送达
        $res = $progress_model->riderGoodsSuccess($order['uniacid'],$order['order_code']);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-04 11:01
     * @功能说明:员工给用户减菜
     */
    public function delFoods(){

        $input = $this->_input;

        $order_goods_model = new OrderGoods();

        Db::startTrans();

        foreach ($input['data'] as $vs){
            //子订单
            $order_goods = $order_goods_model->orderInfo(['id'=>$vs['order_goods_id']]);

            if($vs['num']>$order_goods['goods_num']){

                $this->errorMsg('超出数量');
            }
            //订单详情
            $order = $this->order_model->orderInfo(['id'=>$order_goods['order_id']]);

            if($order['pay_type']!=0||$order['order_type']!=1){

                $this->errorMsg('订单类型或状态错误');
            }

            $pay_price     = $order_goods['pay_price']/$order_goods['goods_num']*$vs['num'];

            $lunch_price   = $order_goods['lunch_price']/$order_goods['goods_num']*$vs['num'];

            $feeding_price = $order_goods['feeding_price']/$order_goods['goods_num']*$vs['num'];

            $weight        = $order_goods['weight']/$order_goods['goods_num']*$vs['num'];
            //修改订单情况
            $order_update = [

                'pay_price'           => round($order['pay_price']-$pay_price,2),
                //商品原价
                'init_goods_price'    => round($order['init_goods_price']-$order_goods['goods_price']*$vs['num'],2),

                'init_price'          => round($order['init_price']-$order_goods['goods_price']*$vs['num'],2),

                'total_lunch_price'   => round($order['total_lunch_price']-$lunch_price,2),

                'weight'              => $order['weight']-$weight,

                'total_feeding_price' => round($order['total_feeding_price']-$feeding_price,2),

                'goods_price'         => round($order['goods_price']-$pay_price,2),

                'member_price'        => round($order['member_price']-($order_goods['goods_price']-$order_goods['member_price'])*$vs['num'],2),
            ];


            $res = $this->order_model->orderUpdate(['id'=>$order_goods['order_id']],$order_update);

            if($res!=1){

                Db::rollback();

                $this->errorMsg('减餐失败');
            }
            //减菜品数量 修改子订单情况
            if($vs['num']<$order_goods['goods_num']){

                $update = [

                    'pay_price'     => round($order_goods['pay_price'] - $pay_price,2),

                    'lunch_price'   => round($order_goods['lunch_price'] - $lunch_price,2),

                    'feeding_price' => round($order_goods['feeding_price'] - $feeding_price,2),

                    'weight'        => round($order_goods['weight'] - $weight,2),

                    'goods_num'     => $order_goods['goods_num'] - $vs['num'],

                    'can_refund_num'=> $order_goods['goods_num'] - $vs['num'],

                ];

                $res = $order_goods_model->orderUpdate(['id'=>$vs['order_goods_id']],$update);

            }else{
                //删除菜品
                $res = $order_goods_model->where(['id'=>$vs['order_goods_id']])->delete();
            }

            if($res!=1){

                Db::rollback();

                $this->errorMsg('减餐失败');
            }

            $order_count = $order_goods_model->where(['order_id'=>$order_goods['order_id']])->count();

            if(empty($order_count)){

                Db::rollback();

                $this->errorMsg('若要全部取消菜品，请直接取消订单');
            }

            $goods_model = new Goods();
            //商品信息
            $goods = $goods_model->goodsInfo(['id'=>$order_goods['goods_id']]);

            $update = [
                //减销量
                'true_sale_num' => ($goods['true_sale_num'] - $vs['num'])>0?$goods['true_sale_num'] - $vs['num']:0,
                //减销售额
                'sale_cash'     => ($goods['sale_cash'] - $pay_price)>0?($goods['sale_cash'] - $pay_price):0,
            ];

            $res = $goods_model->where(['id'=>$order_goods['goods_id']])->update($update);

        }

        Db::commit();

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-05 10:25
     * @功能说明:员工端订单列表
     */
    public function houseOrderList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $store = $this->staff_model->getStaffStore($this->getUserId(),3);
        //员工端
        if($store['role']==0){

            $dis[] = ['a.staff_id','=',$this->getUserId()];

        }else{
        //管理员端
            $dis[] = ['a.store_id','=',$this->store_info['id']];
        }

        $dis[] = ['a.order_type','=',1];
        //状态
        if(isset($input['pay_type'])&&$input['pay_type']!=999&&empty($input['confirm'])){

            $dis[] = ['a.pay_type','=',$input['pay_type']];
            //已经确认的
            $dis[] = ['a.confirm','=',1];

        }
        //待确认
        if(!empty($input['confirm'])){

            $dis[] = ['a.confirm','=',0];
        }
        //check_out = 1或者2 是先就餐后付款 transaction_id<>0 是已经支付过的 这两个条件任意满足一个即可
        $where1[] = ['a.check_out','<>',0];

        $where1[] = ['a.transaction_id','<>',0];

        $where = [];
        //商品
        if(!empty($input['goods_name'])){

            $where[] = ['f.goods_name','like','%'.$input['goods_name'].'%'];

            $where[] = ['a.order_code','like','%'.$input['goods_name'].'%'];
        }

        $data = $this->order_model->orderList($dis,$where,$where1);

        $data['confirm_count'] = $this->order_model->confirmCount($this->getUserId());

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-05 11:42
     * @功能说明:处理
     */
    public function doConfirm(){

        $input = $this->_input;

        $this->order_model->orderUpdate(['id'=>$input['id']],['confirm'=>1]);

        $order_goods_model = new OrderGoods();

        $order_goods_model->orderUpdate(['order_id'=>$input['id'],'confirm'=>0],['confirm'=>1,'confirm_user'=>$this->getUserId()]);

        return $this->success(true);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-05 14:27
     * @功能说明:员工订单排名
     */
    public function staffOrderTop(){

        $input = $this->_param;

        $dis[] = ['a.store_id','=',$this->store_info['id']];

        $dis[] = ['e.pay_type','=',7];

        $staff_model = new StoreStaff();

        $data['top_three'] = $staff_model->orderCountTopThree($dis);

        if(!empty($input['title'])){

            $dis[] = ['b.name','like','%'.$input['title'].'%'];
        }

        $data['top_list'] = $staff_model->orderCountTopList($dis);

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-03-05 14:57
     * @功能说明:员工排名详情
     */
    public function staffOrderTopInfo(){

        $input = $this->_param;

        $dis[] = ['a.store_id','=',$this->store_info['id']];

        $dis[] = ['e.pay_type','=',7];

        $staff_model = new StoreStaff();

        $top_list = $staff_model->orderCountTopThree($dis,10000);

        $staff = array_column($top_list,'staff_id');

        $key   = array_search($input['staff_id'],$staff);
        //排名
        $data['top'] = $key+1;
        //金额
        $data['all_price']      = $this->order_model->staffOrderPrice($input['staff_id']);
        //单数
        $data['all_count']      = $this->order_model->staffOrderCount($input['staff_id']);
        //服务人数
        $data['all_user_num']   = $this->order_model->staffUserPrice($input['staff_id']);
        //今天
        $data['today_price']    = $this->order_model->staffOrderPrice($input['staff_id'],1);

        $data['today_count']    = $this->order_model->staffOrderCount($input['staff_id'],1);

        $data['today_user_num'] = $this->order_model->staffUserPrice($input['staff_id'],1);
        //昨天
        $data['yeday_price']    = $this->order_model->staffOrderPrice($input['staff_id'],2);

        $data['yeday_count']    = $this->order_model->staffOrderCount($input['staff_id'],2);

        $data['yeday_user_num'] = $this->order_model->staffUserPrice($input['staff_id'],2);

        $data['staff_name']    = lbUserName($input['staff_id']);

        $data['staff_avatar']  = lbUserAvatar($input['staff_id']);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-08 16:07
     * @功能说明:员工手动结束订单
     */
    public function endOrder(){

        $input = $this->_input;

        $order = $this->order_model->orderInfo(['id'=>$input['order_id']]);
        //
        if($order['pay_type']!=0){

            $this->errorMsg('订单状态错误');
        }

        if($order['order_type']!=1){

            $this->errorMsg('该订单不是堂食');

        }

        $update = [

            'end_staff' => $this->getUserId(),

            'staff_text'=> $input['staff_text']
        ];

        Db::startTrans();

        $res = $this->order_model->orderUpdate(['id'=>$input['order_id']],$update);

        if($res!=1){

            Db::rollback();

            $this->errorMsg('提交失败');

        }

        $this->order_model->orderResult($order['order_code'],$order['order_code']);

        Db::commit();

        return $this->success($res);


    }

    /**
     * @author chenniang
     * @DataTime: 2021-03-08 16:31
     * @功能说明:取消订单
     */
    public function cancelOrder(){

        $input = $this->_input;

        $order = $this->order_model->orderInfo(['id'=>$input['order_id']]);
        //
        if($order['pay_type']!=0){

            $this->errorMsg('订单状态错误');
        }

        if($order['order_type']!=1){

            $this->errorMsg('该订单不是堂食');

        }

        $update = [

            'end_staff' => $this->getUserId(),

            'staff_text'=> $input['staff_text'],

        ];
        Db::startTrans();

        $res = $this->order_model->orderUpdate(['id'=>$input['order_id']],$update);

        if($res!=1){

            Db::rollback();

            $this->errorMsg('提交失败1');

        }

        $res = $this->order_model->cancelOrder($input['order_id']);

        if($res!=1){

            Db::rollback();

            if(!empty($res['code'])){

                $this->errorMsg($res['msg']);

            }

            $this->errorMsg('提交失败');

        }

        Db::commit();

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-10 17:16
     * @功能说明:修改餐位费
     */
    public function updateMealFee(){

        $input = $this->_input;

        $order = $this->order_model->orderInfo(['id'=>$input['order_id']]);
        //
        if($order['pay_type']!=0){

            $this->errorMsg('订单状态错误');
        }

        if($order['order_type']!=1){

            $this->errorMsg('该订单不是堂食');

        }

        $meal_fee  = round($order['meal_fee']/$order['user_num']*$input['user_num'],2);

        $pay_price = round($order['pay_price']-$order['meal_fee']+$meal_fee,2);

        $init_price = round($order['init_price']-$order['meal_fee']+$meal_fee,2);

        $update = [

            'meal_fee'  => $meal_fee,

            'pay_price' => $pay_price,

            'user_num'  => $input['user_num'],

            'init_price'  => $init_price,
        ];

        $res = $this->order_model->orderUpdate(['id'=>$input['order_id']],$update);

        return $this->success($res);

    }





}
